package com.yogee.topseller.activity.my;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.SharedPreferencesUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.view.impl.GuideLoginRegist;
import com.yogee.topseller.adapter.ContractAddressManageRvAdapter;
import com.yogee.topseller.bean.ContractAddressBean;
import com.yogee.topseller.bean.DeleteAddressBean;
import com.yogee.topseller.bean.SetDefaultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.BasicDialog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ContractAddressManageActivity extends HttpActivity {

    @BindView(R.id.cam_title_layout)
    TitleLayout camTitleLayout;
    @BindView(R.id.cam_rv)
    RecyclerView camRv;
    private ContractAddressManageRvAdapter adapter;
    private List<ContractAddressBean.AddressListBean> data = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_contract_address_manage;
    }

    @Override
    protected void initView() {
        camTitleLayout.setActivity(this);
        camTitleLayout.setTitle("合同地址管理");
        camRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ContractAddressManageRvAdapter(this);
        camRv.setAdapter(adapter);

        //设置默认地址
        adapter.setClickListener(new ContractAddressManageRvAdapter.ClickListener() {
            @Override
            public void clickDefault(String id,int pos) {
                setDefault(AppUtil.getUserId(ContractAddressManageActivity.this),id);
            }
        });
        //删除
        adapter.setDeleteListener(new ContractAddressManageRvAdapter.DeleteListener() {
            @Override
            public void delete(final String id, final int position) {
                BasicDialog.Builder builder = new BasicDialog.Builder(ContractAddressManageActivity.this);
                builder.setMessage("是否删除？")
                        .setPositiveButton("确定", new android.app.AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteAddress(AppUtil.getUserId(ContractAddressManageActivity.this),id,position);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new android.app.AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                builder.create().show();

            }
        });

        adapter.setEditListener(new ContractAddressManageRvAdapter.EditListener() {
            @Override
            public void edit(String id, ContractAddressBean.AddressListBean addressListBeen,int position) {
                startActivity(new Intent(ContractAddressManageActivity.this,AddAddressActivity.class).putExtra("edit","编辑").
                        putExtra("list", (Serializable) addressListBeen));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        contractAddress(AppUtil.getUserId(this));
    }

    private void contractAddress(String userId) {
        HttpManager.getInstance().contractAddress(userId)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ContractAddressBean>() {
            @Override
            public void onNext(ContractAddressBean o) {
                ContractAddressManageActivity.this.loadingFinished();
                data = o.getAddressList();
                adapter.addData(data);
            }
        }, this));
    }

    private void setDefault(String userId, String id) {
        HttpManager.getInstance().setDefault(userId, id)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<SetDefaultBean>() {
            @Override
            public void onNext(SetDefaultBean o) {
                ContractAddressManageActivity.this.loadingFinished();
                contractAddress(AppUtil.getUserId(ContractAddressManageActivity.this));
            }
        }, this));
    }

    private void deleteAddress(String userId, String id, final int position) {
        HttpManager.getInstance().deleteAddress(userId, id)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<DeleteAddressBean>() {
            @Override
            public void onNext(DeleteAddressBean o) {
                ContractAddressManageActivity.this.loadingFinished();
                data.remove(position);
                adapter.notifyDataSetChanged();
            }
        }, this));
    }

}
